#include "stdafx.h"
#include "problem_solver_core.h"
#include "problem.h"
#include "solver.h"
#include "Output.h"
#include "tag_info.h"

namespace SimpleGUI
{
	// Constructor and Destructor

	template <class T, class U> Output<T, U>::Output(nnse::solver<T, U>^ p)
	: parent(p)
	{
		InitializeComponent();
		add_grid();
		show_vector();
		this->Show();
	};
	
	template <class T, class U> Output<T, U>::~Output()
	{
		if (components)
		{
			delete components;
		}
	};

	// GUI Functions

	template <class T, class U> System::Void Output<T, U>::form_closed(System::Object^  sender, System::Windows::Forms::FormClosedEventArgs^  e)
	{
		parent->invalidate_output_form();
	};

	template <class T, class U> System::Void Output<T, U>::add_grid()
	{
		unsigned int x_offset=textBox->Location.X;
		unsigned int y_offset=textBox->Location.Y + textBox->Size.Height;
		unsigned int x_size=20;
		unsigned int y_size=20;
		this->SuspendLayout();
		grid=gcnew array<System::Windows::Forms::Label^>(parent->get_parent_problem()->get_number_of_targets());
		for(unsigned int el=0; el<parent->get_parent_problem()->get_number_of_targets(); el++)
		{
			grid[el]=gcnew System::Windows::Forms::Label();
			tag_info^ tag=gcnew tag_info(el, parent->get_parent_problem()->get_wrap_target());
			grid[el]->Location=System::Drawing::Point(x_offset + (tag->get_column_index() * x_size), y_offset + (tag->get_row_index() * y_size));
			grid[el]->Size=System::Drawing::Size(x_size, y_size);
			grid[el]->BackColor=System::Drawing::SystemColors::Control;
			grid[el]->BorderStyle=System::Windows::Forms::BorderStyle::FixedSingle;
			this->Controls->Add(grid[el]);
		}
		this->ResumeLayout(false);
	};

	template <class T, class U> System::Void Output<T, U>::remove_grid()
	{
		this->SuspendLayout();
		if (grid)
		{
			textBox->Text="";
			for(int el=0; el<grid->Length; el++)
			{
				this->Controls->Remove(grid[el]);
				delete grid[el];
			}
			delete grid;
		}
		this->ResumeLayout(false);
	};

	template <class T, class U> System::Void Output<T, U>::show_vector()
	{
		this->SuspendLayout();
		if (grid)
		{
			unsigned int max_el=parent->normalise_output_vector();
			textBox->Text="";
			for(unsigned int el=0; el<parent->get_parent_problem()->get_number_of_targets(); el++)
			{
				U val=parent->get_output_value(el);
				if (val>=0 && val<=1)
				{
					int a=static_cast<int>(val * System::Drawing::SystemColors::ControlDarkDark.A);
					int r=static_cast<int>(val * System::Drawing::SystemColors::ControlDarkDark.R);
					int g=static_cast<int>(val * System::Drawing::SystemColors::ControlDarkDark.G);
					int b=static_cast<int>(val * System::Drawing::SystemColors::ControlDarkDark.B);
					grid[el]->BackColor=System::Drawing::Color::FromArgb(a, r, g, b);
				}
				if (el==max_el)
				{
					System::String^ target_comment=parent->get_parent_problem()->target_comments[el];
					grid[el]->Text=target_comment;
					textBox->Text=target_comment;
				}
				else
				{
					grid[el]->Text="";
				}
			}
			this->Refresh();
		}
		this->ResumeLayout(false);
	};

	// Explicit Instantiation

	template ref class Output<bool, double>;
	template ref class Output<unsigned char, double>;
}
